﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Pay.Core;
namespace Pay.Token.Controllers.Api
{
    /// <summary>
    /// 文章
    /// </summary>
    [RoutePrefix("article")]
    public class ArticleController : BaseController {

        private IArticleService _iArticleService;
        /// <summary>
        /// 文章
        /// </summary>
        /// <param name="iArticleService"></param>
        public ArticleController(IArticleService iArticleService) {
            _iArticleService = iArticleService;
        }

        /// <summary>
        /// 获取文章分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_articles"), HttpGet, AllowAnonymous]
        public ResultMessage<PagedResult<article_dto_ex>> GetArticles([FromUri]AppArticleQuery query) {
            var list = _iArticleService.GetArticles(query);
            var result = new PagedResult<article_dto_ex>(list.rows, list.total_result, list.page, list.size);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获取我的文章分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("my_articles"), HttpGet]
        public ResultMessage<PagedResult<article_dto_ex>> MyArticles([FromUri]PageQuery query) {
            var list = _iArticleService.GetArticles(new AppArticleQuery { merchant_id = UserId, is_highlight = 0, page = query.page, size = query.size});
            var result = new PagedResult<article_dto_ex>(list.rows, list.total_result, list.page, list.size);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获取我的参与文章分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("join_articles"), HttpGet]
        public ResultMessage<PagedResult<article_dto_ex>> JoinArticles([FromUri]PageQuery query) {
            var list = _iArticleService.GetArticles(new AppArticleQuery { my_id = UserId, is_highlight = 0 , page = query.page, size = query.size});
            var result = new PagedResult<article_dto_ex>(list.rows, list.total_result, list.page, list.size);
            return ResultToJson.Json(result);
        }


        /// <summary>
        /// 用户发帖
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("add_article"), HttpPost]
        public ResultMessage<string> Addarticle([FromBody]article_model model) {
            if (!ModelState.IsValid)
                return ResultToJson.Json<string>(ModelError);
            throw new ServerErrorException("该功能已暂停开放");
            model.merchant_id = UserId;
            var result = _iArticleService.Save(model);
            return ResultToJson.Json(result > 0 ? "发帖成功" : "发帖失败", "");
        }

        /// <summary>
        /// 帖子详情
        /// </summary>
        /// <param name="article_id">文章id</param>
        /// <returns></returns>
        [Route("article_detail"), HttpGet, AllowAnonymous]
        public ResultMessage<article_dto_ex> GetOneArticle(long article_id) {
            var result = _iArticleService.GetOneArticle(article_id);
            return ResultToJson.Json(result);
        }


        /// <summary>
        /// 用户删除自己帖子
        /// </summary>
        /// <param name="article_id">帖子id</param>
        /// <returns></returns>
        [Route("delete_article"), HttpGet]
        public ResultMessage<string> DeleteArticle(long article_id) {
            var result = _iArticleService.UpdateArticle(new article_status { id = article_id, is_highlight = false, is_top = false , status = ArticleStatus.Delete});
            return ResultToJson.Json(result > 0 ? "删除成功" : "删除失败","");
        }

        /// <summary>
        /// 回帖
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_reply_article"), HttpGet, AllowAnonymous]
        public ResultMessage<PagedResult<reply_article_ex>> GetReplyArticles([FromUri]ReplyArticleQuery query) {
            if(UserId > 0)
                query.merchant_id = UserId;
            var list = _iArticleService.GetReplys(query);
            var result = new PagedResult<reply_article_ex>(list.rows, list.total_result, list.page, list.size);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 用户删除自己回帖
        /// </summary>
        /// <param name="reply_article_id">回帖id</param>
        /// <returns></returns>
        [Route("delete_reply_article"), HttpGet]
        public ResultMessage<string> DeleteReply(long reply_article_id) {
            var result = _iArticleService.DeleteReplyArticle(reply_article_id , UserId);
            return ResultToJson.Json(result > 0 ? "删除成功" : "删除失败","");
        }

        /// <summary>
        /// 用户回帖
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("reply_article"), HttpPost]
        public ResultMessage<string> ReplyArticle([FromBody]reply_article_model model) {
            model.merchant_id = UserId;
            var result = _iArticleService.Save(model);
            return ResultToJson.Json(result > 0 ? "回帖成功" : "回帖失败", "");
        }


    }
}